import { _decorator, Component, Node, random,Label, Tween, Vec3,Animation, director ,  AudioClip, AudioSource, assert  } from 'cc';
const { ccclass, property } = _decorator;
import Common from './Common';      

@ccclass('MathPlay')
export class MathPlay extends Component {
    
    
   
    //最大的数字
    private maxNumber: number = 10;
    //题目数量
    private countNumber: number = 10;
    
    private opt1:number=1;//加
    private opt2:number=-1;//减
    private opt3:number=-1;//乘
    private opt4:number=-1//除

    //当前数量
    private curNumber:number=0;

    private optlist:number[] = []

    private result1:String="";
    private result2:String="";
    private result3:String="";
    private result4:String="";


    private optNumberA:number=0;
    private optNumberB:number=0;
    private opt:String="1";
    private optResult:number;
    private optQuesion:string="";

     //速度
     private speed:number=1;
    //答案Index
    private questionIndex=0;

    //time
    private time:number=90;

    //最大数
    @property(Label)
    private questionLabel: Label = null;

    //最大数
    @property(Label)
    private LabelResultA: Label = null;
    //最大数
    @property(Label)
    private LabelResultB: Label = null;
    //最大数
    @property(Label)
    private LabelResultC: Label = null;
    //最大数
    @property(Label)
    private LabelResultD: Label = null;

    @property(Label)
    private LabelTime: Label = null;

    @property(Label)
    private LabelCount: Label = null;


    @property(Node)
    private cloudQuestion: Node = null;


    @property(Node)
    private questionA: Node = null;

    @property(Node)
    private questionB: Node = null;

    @property(Node)
    private questionC: Node = null;

    @property(Node)
    private questionD: Node = null;


    @property(Node)
    private dialog: Node = null;

    private state:String="play";



    @property(AudioClip)
    public clip: AudioClip = null!;   

    @property(AudioClip)
    public clipWin: AudioClip = null!;   

    @property(AudioClip)
    public clipWrong: AudioClip = null!;   


    @property(AudioSource)
    public _audioSource: AudioSource = null!;



    @property(Node)
    private failDiv: Node = null;

    @property(Node)
    private stopDiv: Node = null;

    @property(Node)
    private sucessDiv: Node = null;
    
    
    

    

    //读取配置信息
    start() {


          // 获取 AudioSource 组件
          const audioSource = this.node.getComponent(AudioSource)!;
          // 检查是否含有 AudioSource，如果没有，则输出错误消息
           assert(audioSource);
            // 将组件赋到全局变量 _audioSource 中
            this._audioSource = audioSource;



        this.speed=Common.speed;
        this.maxNumber=Common.maxNumber;
        this.countNumber=Common.countNumber;

        this.opt1=Common.opt1;
        this.opt2=Common.opt2;
        this.opt3=Common.opt3;
        this.opt4=Common.opt4;

        if(this.opt1==1){
            this.optlist.push(1);
        }
        if(this.opt2==1){
            this.optlist.push(2);
        }
        if(this.opt3==1){
            this.optlist.push(3);
        }
        if(this.opt4==1){
            this.optlist.push(4);
        }

        console.log(this.optlist);
        this.initGame();


       

    }

    //倒计时
    private doCountdownTime() {
        //每秒更新显示信息
        if (this.time> 0) {
            if(this.state!="stop"){
                this.time -= 1;
            }
           
            this.countDownShow(this.time);
        }else{

            //失败
            this.fail();
        }

    }
    private countDownShow(temp) {
        if (temp <= 0) {

            this.LabelTime.string=0+'';
            //倒计时结束
            this.unschedule(this.doCountdownTime);
            //执行失败操作
            this.fail();
        }else{
            this.LabelTime.string=temp+'';
        }
    }

    private setState(state:String){
        this.state=state;
        if(state=="stop"){
             
        }

    }
    
    private questionNew(){


      if(!this.checkContinue()){
        this.success();
        return;

      }

      //this._audioSource.play();
      this._audioSource.playOneShot(this.clip, 1);

      const tw1 = new Tween(this.cloudQuestion)
      let x=this.cloudQuestion.position.x;
      let y=this.cloudQuestion.position.y;
      let z=this.cloudQuestion.position.z;
  
       tw1.to(0.001, { position: new Vec3(500, 500, z) }, { easing: 'bounceInOut' })
      .to(0.5, { position: new Vec3(x, y, z)}, { easing: 'smooth' })
      .union()
      .start();

        console.log('动画动画')

        this.LabelResultA.string='0';
        this.LabelResultB.string='0';
        this.LabelResultC.string='0';
        this.LabelResultD.string='0';


        let optLength=this.optlist.length;
        let opt=this.optlist[this.randomInt(0,optLength-1)];
        this.opt=opt+"";

        console.log('cur opt'+this.opt)

         //设置答案位置
         this.questionIndex=this.randomInt(1,4);

         //另外三个答案
         let resultArr:number[] = [];

        //加
        if(opt==1){
            this.optNumberA=this.randomInt(0,this.maxNumber);
            this.optNumberB=this.randomInt(1,this.maxNumber);
            this.optResult=this.optNumberA+this.optNumberB;

            this.optQuesion=this.optNumberA+" + "+this.optNumberB+" = ?";


             //结果*2的范围内
             resultArr.push(this.optResult);
             let low=this.optResult;
             if(low>10){
                low=low-10;
             }
             while(resultArr.length<4){
                let temp=this.randomInt(low,this.optResult+10);
                if(resultArr.indexOf(temp)<0){
                    resultArr.push(temp);
                }
             }

        }
        //减
        if(opt==2){

            this.optResult=this.randomInt(1,this.maxNumber);
            this.optNumberB=this.randomInt(0,this.optResult);
            this.optNumberA=this.optResult+this.optNumberB;

            this.optQuesion=this.optNumberA+" - "+this.optNumberB+" = ?";


             //减数范围内
             resultArr.push(this.optResult);

             let low=this.optResult;
             if(low>10){
                low=low-10;
             }

             while(resultArr.length<4){
                let temp=this.randomInt(low,this.optResult+10);
                if(resultArr.indexOf(temp)<0){
                    resultArr.push(temp);
                }
             }
           
        }

        //乘
        if(opt==3){
            this.optNumberA=this.randomInt(1,this.maxNumber);
            this.optNumberB=this.randomInt(1,this.maxNumber);
            this.optResult=this.optNumberA*this.optNumberB;

            this.optQuesion=this.optNumberA+" × "+this.optNumberB+" = ?";


             //结果*2的范围内
             resultArr.push(this.optResult);
             while(resultArr.length<4){
                let temp=this.randomInt(0,this.optResult*2+10);
                if(resultArr.indexOf(temp)<0){
                    resultArr.push(temp);
                }
             }

    
         }
         //除
         if(opt==4){

            let a=this.randomInt(1,this.maxNumber);
            let b=this.randomInt(1,this.maxNumber);
            let c=a*b;
           
            this.optNumberA=c;
            this.optNumberB=a;
            this.optResult=b;
            this.optQuesion=this.optNumberA+" ÷ "+this.optNumberB+" = ?";


              //结果*2的范围内
              resultArr.push(this.optResult);
              while(resultArr.length<4){
                let temp=this.randomInt(0,this.optResult*2+10);
                if(resultArr.indexOf(temp)<0){
                    resultArr.push(temp);
                }
             }


         }

         this.questionLabel.string=this.optQuesion;

         if(this.questionIndex==1){
            this.LabelResultA.string=this.optResult+'';
             this.LabelResultB.string=resultArr[1]+'';
             this.LabelResultC.string=resultArr[2]+'';
             this.LabelResultD.string=resultArr[3]+'';


         }
         if(this.questionIndex==2){
            this.LabelResultB.string=this.optResult+'';

             this.LabelResultA.string=resultArr[1]+'';
             this.LabelResultC.string=resultArr[2]+'';
             this.LabelResultD.string=resultArr[3]+'';

         }
         if(this.questionIndex==3){
            this.LabelResultC.string=this.optResult+'';


           
             this.LabelResultA.string=resultArr[1]+'';
             this.LabelResultB.string=resultArr[2]+'';
             this.LabelResultD.string=resultArr[3]+'';

         }
         if(this.questionIndex==4){
            this.LabelResultD.string=this.optResult+'';

           

             this.LabelResultA.string=resultArr[1]+'';
             this.LabelResultB.string=resultArr[2]+'';
             this.LabelResultC.string=resultArr[3]+'';

         }

         //设置状态
         this.curNumber=this.curNumber+1;
         this.LabelCount.string="答题数量："+this.curNumber+"/"+this.countNumber;
         //this.LabelTime.string="答题时间：";





    }


    //答案A
    private onBtnClickA(){
        if(this.questionIndex==1){
            this.questionNew();
        }else{
            console.log('lose');
            this.fail();
        }       
    }
    //答案B
    private onBtnClickB(){
        if(this.questionIndex==2){
            this.questionNew();
        }else{
            console.log('lose');
            this.fail();
        }    
    }
    //答案C
    private onBtnClickC(){
        if(this.questionIndex==3){
            this.questionNew();
        }else{
            console.log('lose');
            this.fail();
        }    
    }
    //答案D
    private onBtnClickD(){
        if(this.questionIndex==4){
            this.questionNew();
        }else{
            console.log('lose');
            this.fail();
        }    
    }

  

    //首页
    private onBtnHome(){
        director.loadScene("mathmenu");
    }
    //开始游戏
    private onBtnPlay(){
        const tw1 = new Tween(this.dialog)
        let x=this.dialog.position.x;
        let y=this.dialog.position.y;
        let z=this.dialog.position.z;
    
        tw1.to(0.2, { position: new Vec3(0, 1000, z)}, { easing: 'smooth' })
        .union()
        .start();

        
        this.state="play";


    }
    //重新开始
    private onBtnRePlay(){
        //director.loadScene("mathmenu");
        this.initGame();
        this.onBtnPlay();
    }

    private initGame(){
        //快，1个题目3秒
        if(this.speed==1){
           this.time=this.countNumber*3;
        }
        //中，1个题目6秒
        if(this.speed==2){
            this.time=this.countNumber*6;
        }
        //慢，1个题目9秒
        if(this.speed==3){
            this.time=this.countNumber*9;
        }
        //调整理当前数量
        if(this.curNumber>0){
            this.unschedule(this.doCountdownTime);
        }
        this.curNumber=0;
        this.LabelTime.string=this.time+'';

         //定时任务，导计时
         this.schedule(this.doCountdownTime, 1);

         //默认出一题目
         this.questionNew();


    }
    

    //随机得到数字
    private  randomInt(min, max) {   
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    private checkContinue(){

        if(this.curNumber<this.maxNumber){
            return true;
        }
        return false;

    }

 

    //暂停
    private onBtnStop(){

      const tw1 = new Tween(this.dialog)
      let x=this.dialog.position.x;
      let y=this.dialog.position.y;
      let z=this.dialog.position.z;

      tw1.to(0.2, { position: new Vec3(0, 0, z)}, { easing: 'smooth' })
      .union()
      .start();

      this.state="stop";

      
      this.failDiv.active=false;
      this.sucessDiv.active=false;
      this.stopDiv.active=true;

    }
    private success(){



        const tw1 = new Tween(this.dialog)
        let x=this.dialog.position.x;
        let y=this.dialog.position.y;
        let z=this.dialog.position.z;
  
        tw1.to(0.2, { position: new Vec3(0, 0, z)}, { easing: 'smooth' })
        .union()
        .start();
        this._audioSource.playOneShot(this.clipWin, 1);
        console.log('挑战成功')

        this.failDiv.active=false;
        this.sucessDiv.active=true;
        this.stopDiv.active=false;

        


    }

    private fail(){

        const tw1 = new Tween(this.dialog)
        let x=this.dialog.position.x;
        let y=this.dialog.position.y;
        let z=this.dialog.position.z;
  
        tw1.to(0.2, { position: new Vec3(0, 0, z)}, { easing: 'smooth' })
        .union()
        .start();

        this._audioSource.playOneShot(this.clipWrong, 1);
        console.log('挑战失败')

        this.failDiv.active=true;
        this.sucessDiv.active=false;
        this.stopDiv.active=false;

    }

   
}

